package sg.techstudio.suntec.validator;

import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import sg.techstudio.suntec.controller.BaseController;
import sg.techstudio.suntec.dao.EmailDao;
import sg.techstudio.suntec.dao.EventContactDao;
import sg.techstudio.suntec.dao.RadiusDao;
import sg.techstudio.suntec.object.CPLoginObj;

@Component
public class CpValidatorMvc {
	private static final Log log = LogFactory.getLog(CpValidatorMvc.class);
	public static final String ERRORCODE_REQUIRED_FIELD = "required.field";
	
	@Autowired
	private EventContactDao eDao;
	
	@Autowired
	private RadiusDao radiusDao;
	
	@Autowired
	private EmailDao emailDao;
	
	@Autowired
	private MessageSource messageSource;
	
	public void validateLoginForm(CPLoginObj cp, Model model) {
		log.debug("validateLoginForm() entry");
		
		if(StringUtils.isBlank(cp.getPassword())) {
			addMessage(ERRORCODE_REQUIRED_FIELD, model);
			return;
		}
		if(!StringUtils.equalsIgnoreCase("free", cp.getPassword())
				&& !eDao.checkPasswordExist(cp.getPassword())) {
			addMessage("cp.invalidcredential", model);
			return;
		}
		/*
		switch(wifiAccountObj.getPackageType()) {
		case CustomTagUtil.PACKAGE_GOLD:
			if(!isGoldSsid(cp.getSsid())) {
				addMessage("cp.goldpackage", model);
				return;	
			}
			break;
		case CustomTagUtil.PACKAGE_SILVER:
			if(!isSilverSsid(cp.getSsid())) {
				addMessage("cp.silverpackage", model);
				return;	
			}
			break;
		default:
			if(!isChcSsid(cp.getSsid())) {
				addMessage("cp.chcpackage", model);
				return;	
			}
			break;
		}
		if(radiusDao.countActiveSession(cp.getUsername(), cp.getMac()) >= wifiAccountObj.getMaxDevices()) {
			addMessage("cp.devicelimit", model);
			return;
		}
		RestEventContactObj re = eDao.findEventByWifiAccount(wifiAccountObj);
		cp.setType(re.getType());
		Calendar now = Calendar.getInstance();
		if(re==null || !re.isActive() || now.getTime().after(re.getActivateEndDate())
				|| now.getTime().before(re.getActivateStartDate())) {
			addMessage("cp.disabled", model);
			return;
		}
		*/
		//send vip alert notification if internal email is not blank and type is contact
//		if(re.getType()==1 && !StringUtils.isBlank(re.getInternalEmail()) 
//				&& BaseValidator.EMAIL_PATTERN.matcher(re.getInternalEmail()).matches()) {
//			try { //surround with try catch block locally
//				emailDao.sendVipAlertEmail(cp, re.getInternalEmail());
//			} catch(Exception e) {
//				log.error("exception when sending alert email"+e, e);
//			}
//		}
	}
	
	public boolean isGoldSsid(String ssid) {
		return StringUtils.containsIgnoreCase(ssid, "GOLD_W") 
				||StringUtils.containsIgnoreCase(ssid, "G_W");
	}
	public boolean isSilverSsid(String ssid) {
		return StringUtils.containsIgnoreCase(ssid, "SILVER_W") 
				||StringUtils.containsIgnoreCase(ssid, "S_W");
	}
	public boolean isChcSsid(String ssid) {
		return StringUtils.containsIgnoreCase(ssid, "CHC_W");
	}
	
	protected void addMessage(String messageCode, Model model) {
		model.addAttribute(BaseController.PARAM_ERRMSG
				, messageSource.getMessage(messageCode, null, Locale.ENGLISH));
	}

}
